package org.sundatagroup.aurora.core.entity.pojo;

import org.sundatagroup.aurora.core.entity.IJsonEntity;
import org.sundatagroup.aurora.core.entity.model.ModuleModel;

import com.google.gson.JsonObject;

/**
 * <p>项目 {@link Project} 所包含的模块 Module
 * </p>
 * 
 * @version 0.1.1
 * 
 * @author CharlesChen
 * @date 2018-03-29
 * @since 0.1
 */
public class Module implements IJsonEntity {
    int mid;
    String name;
    String path;
    JsonObject option;
    
    /**
     * 描述. 这个数据不会传到前端
     */
    String desc;
    
    public Module() {}

    public Module(String name) {
        this(name, null);
    }

    public Module(String name, JsonObject option) {
        this.mid = 0;
        this.name = name;
        this.option = option;
    }
    
    public ModuleModel toModel() {
    	ModuleModel mm = new ModuleModel();
    	mm.setName(name);
    	mm.setMid(mid);
    	mm.setOption(option);
    	mm.setPath(path);
    	mm.setDesc(desc);
    	return mm;
    }
    
    public static Module fromModel(ModuleModel mm) {
    	Module m = new Module(mm.getName(), mm.getOptionToJson());
    	m.setMid(mm.getMid());
    	m.setPath(mm.getPath());
    	m.setDesc(mm.getDesc());
    	return m;
    }

    /**
     * 检查 Module 的参数是否正确
     * @return
     */
    public boolean isValid() {
        return mid > 0 && name.length() != 0 && path.length() != 0 ? true : false;
    }

    public int getMid() {
        return mid;
    }

    public void setMid(int mid) {
        this.mid = mid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public JsonObject getOption() {
        return option;
    }

    public void setOption(JsonObject option) {
        this.option = option;
    }

    public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
    public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	@Override
	public String toString() {
		return "Module [mid=" + mid + ", name=" + name + ", path=" + path + ", option=" + option + "]";
	}

    @Override
	public JsonObject toJson() {
    	JsonObject jo = new JsonObject();
    	jo.addProperty("mid", mid);
    	jo.addProperty("name", name);
    	jo.add("option", option);
    	jo.addProperty("path", path);
    	return jo;
    }
}
